package com.biz.controller.basic;

import com.biz.model.Pmodel.basic.Pbrand;
import com.biz.service.basic.MerchantServiceI;
import com.framework.controller.BaseController;
import com.framework.model.Paging;
import com.framework.model.Params;
import com.framework.utils.SqlFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 商户设置相关
 * Created by liujiajia on 2016/11/14.
 */
@Controller
@RequestMapping("/merchant")
public class MerchantController extends BaseController{


    @Autowired
    private MerchantServiceI merchantService;


    @InitBinder("pbrand")
    public void initBinderFormBean1(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("pbrand.");
    }


    /**
     * 跳转商户设置页面
     * @param mv
     * @return
     */
    @RequestMapping("toMerchant")
    public ModelAndView toMenu(ModelAndView mv){
        mv.setViewName("basic/merchant");
        return mv;
    }
    /**
     * 展示商户列表信息
     * @param response
     * @param request
     */
    @RequestMapping("showMerchant")
    public void showMerchant(HttpServletResponse response, HttpServletRequest request){
        String identity_code=(String)getShiroAttribute("identity_code");

        //必要的分页参数
        SqlFactory factory=new SqlFactory();
        Params sqlParams=factory.getParams();
        sqlParams.setOrder(request.getParameter("order"));
        sqlParams.setPage(Integer.parseInt(request.getParameter("offset")));
        sqlParams.setRows(Integer.parseInt(request.getParameter("limit")));
        sqlParams.setSort(request.getParameter("sort"));
        sqlParams.setIdentity_code(identity_code);
        String name=request.getParameter("name").toString();
        sqlParams.setSearchtext(name);
        Paging paging=merchantService.findMerchantGrid(sqlParams);
        writeJson(paging,response);
    }

    /**
     * 展示商户列表信息
     * @param response
     * @param request
     */
    @RequestMapping("doSave")
    public void doSave(HttpServletResponse response, HttpServletRequest request,Pbrand pbrand)throws Exception{

        String msg=merchantService.saveOrUpdatePbrand(pbrand);
        writeJson(msg,response);
    }


    /**
     * 跳转商户编辑页面
     * @param mv
     * @return
     */
    @RequestMapping("toEdit")
    public ModelAndView toEdit(ModelAndView mv,String id){
        mv.clear();
        mv.addObject("id",id);
        mv.addObject("type","1");
        mv.setViewName("basic/form_edit");
        return mv;
    }

    /**
     * 回显数据
     * @param
     * @return
     */
    @RequestMapping("getById")
    public void getById(HttpServletResponse response,String id)throws Exception{
        Pbrand pbrand=merchantService.getPbranById(id);
        writeJson(pbrand,response);
    }

    /**
     * 删除数据
     * @param
     * @return
     */
    @RequestMapping("delGridById")
    public void delGridById(HttpServletResponse response,String ids)throws Exception{
       String msg=merchantService.delGridById(ids);
        writeJson(msg,response);
    }

    /**
     * 验证商户的登录名是否重复
     * @param
     * @return
     */
    @RequestMapping("checkLoginName")
    public void checkLoginName(HttpServletResponse response,String loginName)throws Exception{
        int count=merchantService.checkLoginName(loginName);
        writeJson(count,response);
    }


}
